package com.hcrazy.manchester.app.controllers.comment;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.Comment;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.CommentService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 评论部分
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api")
public class CommentAPIController {
	
	@Autowired
	private CommentService commentService;
	
	/**
	 * 发表评论
	 * @param 返回当前评论
	 * @return
	 */
	@RequestMapping(value="/ajax/comment",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishCommentWithResult(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int moduleType,
			@RequestParam int stars,
			@RequestParam String commentContent){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不允许发表评论！");
			}
			User user = (User)obj;
			long userId = user.getUserId();
			Comment comment = new Comment();
			comment.setUserId(userId);
			comment.setModuleId(moduleId);
			comment.setCommentContent(commentContent);
			comment.setModuleType(moduleType);
			comment.setStars(stars);
			comment = commentService.addOrUpdateCommentWithResult(comment);
			comment.setUsername(user.getUsername());
			return comment;
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/**
	 * 发表评论
	 * @param
	 * @return
	 */
	@RequestMapping(value="/comment",method={RequestMethod.POST,RequestMethod.GET},produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int moduleType,
			@RequestParam int stars,
			@RequestParam String commentContent){
		try {
			//如果用户未登录，返回错误码
			Object obj = req.getSession().getAttribute("user");
			if(null == obj){
				return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，不允许发表评论！");
			}
			User user = (User)obj;
			long userId = user.getUserId();
			
			Comment comment = new Comment();
			comment.setUserId(userId);
			comment.setModuleId(moduleId);
			comment.setCommentContent(commentContent);
			comment.setModuleType(moduleType);
			comment.setStars(stars);
			
			return commentService.addOrUpdateComment(comment);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/comment/address",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishAddressComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int stars,
			@RequestParam String commentContent){
		return publishComment(req,moduleId,Constant.COMM_ADDRESS_INFO_MODULE,stars,commentContent);
	}
	
	@RequestMapping(value="/comment/simple",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishSimpleComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int stars,
			@RequestParam String commentContent){
		return publishComment(req,moduleId,Constant.COMM_SIMPLE_INFO_MODULE,stars,commentContent);
	}
	
	@RequestMapping(value="/comment/secondary",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishSecondaryComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int stars,
			@RequestParam String commentContent){
		return publishComment(req,moduleId,Constant.COMM_SECONDARY_INFO_MODULE,stars,commentContent);
	}
	
	@RequestMapping(value="/comment/rent",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishRentComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int stars,
			@RequestParam String commentContent){
		return publishComment(req,moduleId,Constant.COMM_RENT_INFO_MODULE,stars,commentContent);
	}
	
	@RequestMapping(value="/comment/ticket",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishTicketComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int stars,
			@RequestParam String commentContent){
		return publishComment(req,moduleId,Constant.COMM_TICKET_INFO_MODULE,stars,commentContent);
	}
	
	@RequestMapping(value="/comment/coupon",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object publishCouponComment(
			HttpServletRequest req,
			@RequestParam int moduleId,
			@RequestParam int stars,
			@RequestParam String commentContent){
		return publishComment(req,moduleId,Constant.COMM_COUPON_INFO_MODULE,stars,commentContent);
	}
	
	@RequestMapping(value="/comments",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getAllComments(
			@RequestParam(required = false) Long moduleId,//模块id
			@RequestParam(required = false) Integer moduleType,//模块类型
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			){
	
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == moduleType){
			moduleType = 0;
		}
		
		if(null == moduleId){
			moduleId = 0L;
		}
		
		try {
			return commentService.getCommonInfos(pageModel, moduleType,moduleId,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/mycomments",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Object getMyComments(
			HttpServletRequest req,
			@RequestParam(required = false) Long moduleId,//模块id
			@RequestParam(required = false) Integer moduleType,//模块类型
			@RequestParam(required = false) Integer pageNum,//第几页
			@RequestParam(required = false) Integer numPerPage//一页请求多少条数据
			){
	
		//如果用户未登录，返回错误码
		Object obj = req.getSession().getAttribute("user");
		if(null == obj){
			return new ErrorMsg(Constant.MEMBER_LOGIN_NOT,"会员未登录，无法查看评论！");
		}
		User user = (User)obj;
		long userId = user.getUserId();
		
		PageModel pageModel = new PageModel();
		if(null != pageNum && pageNum > 0){
			pageModel.setPageNum(pageNum);
		}
		if(null != numPerPage && numPerPage > 0){
			pageModel.setNumPerPage(numPerPage);
		}
		if(null == moduleType){
			moduleType = 0;
		}
		
		if(null == moduleId){
			moduleId = 0L;
		}
		
		try {
			return commentService.getCommonInfos(pageModel, moduleType,moduleId,userId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	/**
	 * 系统参数异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public @ResponseBody ErrorMsg getException(Exception e){
		Log.error(e.getMessage(), e);
		return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
	}
}
